﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate.Tool;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace TitipNyobaYan.Controllers
{
    public static class EntitiesController
    {
        private static ISessionFactory sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (sessionFactory == null)
                    InitializeSessionFactory();
                return sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
            try
            {
                sessionFactory = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008
                        .ConnectionString(@"Server=.\sqlexpress; Database=FPdotnet;Trusted_Connection=true;")
                        .ShowSql()
                    )
                    .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Models.BUKU>())
                    .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, false))
                    .BuildSessionFactory();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
        /*
        public static List<T> getEntities<T>()
        {
            using (var session = OpenSession())
            {
                return session.Query<T>().ToList();
            }
        }
        
        public static void tambah<T>(T input)
        {

            using (var session = OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Save(input);
                    transaction.Commit();
                }
            }
        }
         */
    }
}
